package org.iabako.shared.entity.user;

import com.google.gwt.user.client.rpc.GwtTransient;
import org.hibernate.annotations.Index;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.ClientCommunication;
import org.iabako.shared.entity.business.Payment;
import org.iabako.shared.entity.business.Sale;
import org.iabako.shared.entity.enumeration.EnterpriseTrackType;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * Created by jose on 03/01/15.
 */

@Entity
@Table(name = "ENTERPRISE_TRACK")
public class EnterpriseTrack extends AbstractEntity {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "TYPE", nullable = false)
    @Index(name = "TYPE")
    @Enumerated(EnumType.STRING)
    @NotNull
    private EnterpriseTrackType enterpriseTrackType;

    @Column(name = "TRACKING_DATE", nullable = false)
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    private Date trackingDate;

    @Column(name = "USER_EMAIL", nullable = false)
    @NotNull
    private String userEmail;

    @Column(name = "USER_NAME", nullable = false)
    @NotNull
    private String userName;

    @Column(name = "CLIENT_NUMBER")
    private String clientNumber;

    @Column(name = "CLIENT_NAME")
    private String clientName;

    @Column(name = "SALE_NUMBER")
    private String saleNumber;

    @Column(name = "SALE_AMOUNT")
    private Double saleAmount;

    @Column(name = "PRODUCTS_SERVICES")
    private String productsServices;

    @Column(name = "INVOICE_NUMBER")
    private String invoiceNumber;

    @Column(name = "INVOICE_AMOUNT")
    private Double invoiceAmount;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "ID_CLIENT_COMMUNICATION")
    @GwtTransient
    private ClientCommunication clientCommunication;

    @ManyToOne
    @JoinColumn(name = "ID_ENTERPRISE_BALANCE", nullable = false)
    @NotNull
    private EnterpriseBalance enterpriseBalance;

    public EnterpriseTrack() {
        trackingDate = new Date();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public EnterpriseTrackType getEnterpriseTrackType() {
        return enterpriseTrackType;
    }

    public void setEnterpriseTrackType(EnterpriseTrackType enterpriseTrackType) {
        this.enterpriseTrackType = enterpriseTrackType;
    }

    public Date getTrackingDate() {
        return trackingDate;
    }

    public void setTrackingDate(Date trackingDate) {
        this.trackingDate = trackingDate;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getClientNumber() {
        return clientNumber;
    }

    public void setClientNumber(String clientNumber) {
        this.clientNumber = clientNumber;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getSaleNumber() {
        return saleNumber;
    }

    public void setSaleNumber(String saleNumber) {
        this.saleNumber = saleNumber;
    }

    public Double getSaleAmount() {
        return saleAmount;
    }

    public void setSaleAmount(Double saleAmount) {
        this.saleAmount = saleAmount;
    }

    public String getProductsServices() {
        return productsServices;
    }

    public void setProductsServices(String productsServices) {
        this.productsServices = productsServices;
    }

    public String getInvoiceNumber() {
        return invoiceNumber;
    }

    public void setInvoiceNumber(String invoiceNumber) {
        this.invoiceNumber = invoiceNumber;
    }

    public Double getInvoiceAmount() {
        return invoiceAmount;
    }

    public void setInvoiceAmount(Double invoiceAmount) {
        this.invoiceAmount = invoiceAmount;
    }

    public EnterpriseBalance getEnterpriseBalance() {
        return enterpriseBalance;
    }

    public void setEnterpriseBalance(EnterpriseBalance enterpriseBalance) {
        this.enterpriseBalance = enterpriseBalance;
    }

    public ClientCommunication getClientCommunication() {
        return clientCommunication;
    }

    public void setClientCommunication(ClientCommunication clientCommunication) {
        this.clientCommunication = clientCommunication;
    }

    @Override
    public void copyDatesAsString() {

    }

    @Transient
    public static EnterpriseTrack getFromCommunication(User user, ClientCommunication clientCommunication) {
        EnterpriseTrack track = new EnterpriseTrack();
        track.setUserEmail(user.getEmail());
        track.setUserName(user.getName());

        track.setEnterpriseTrackType(EnterpriseTrackType.communicationTrack);

        track.setClientCommunication(clientCommunication);

        return track;
    }

    public static EnterpriseTrack getFromSale(User user, Sale sale, EnterpriseTrackType type) {
        EnterpriseTrack track = new EnterpriseTrack();
        track.setUserEmail(user.getEmail());
        track.setUserName(user.getName());

        track.setEnterpriseTrackType(type);
        if (sale.getClient() != null) {
            track.setClientNumber(sale.getClient().getNumber());
            track.setClientName(sale.getClient().getName());
        }
        track.setProductsServices(sale.getProductsServicesAsString());
        track.setSaleAmount(sale.calculateAmountFromProductsServices());

        track.setInvoiceNumber(sale.getNumber());
        track.setInvoiceAmount(sale.getRecalculatedTotal());

        return track;
    }

    public static EnterpriseTrack getFromPayment(User user, Payment payment, EnterpriseTrackType type) {
        EnterpriseTrack track = new EnterpriseTrack();
        track.setUserEmail(user.getEmail());
        track.setUserName(user.getName());

        track.setEnterpriseTrackType(type);
        Sale sale = payment.getSale();
        if (payment.getSale().getClient() != null) {
            track.setClientNumber(sale.getClient().getNumber());
            track.setClientName(sale.getClient().getName());
        }
        track.setProductsServices(sale.getProductsServicesAsString());
        track.setSaleAmount(sale.calculateAmountFromProductsServices());

        track.setInvoiceNumber(payment.getInvoiceNumber());
        track.setInvoiceAmount(payment.getAmount());

        return track;
    }
}
